package tinglema.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;
import tinglema.server.common.R;
import tinglema.server.entity.domain.Collect;
import tinglema.server.service.CollectService;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

@RestController
@RequestMapping("/collect")
public class CollectController {

    /**
     * 服务对象
     */
    @Resource
    private CollectService collectService;

    /**
     * 分页查询所有数据
     *
     * @param page    分页对象
     * @param collect 查询实体
     * @return 所有数据
     */
    @GetMapping
    public R selectAll(Page<Collect> page, Collect collect) {
        page.setSize(99);
        return R.success(this.collectService.page(page, new QueryWrapper<>(collect)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R selectOne(@PathVariable Serializable id) {
        return R.success(this.collectService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param collect 实体对象
     * @return 新增结果
     */
    @PostMapping
    public R insert(@RequestBody Collect collect) {
        return R.success(this.collectService.save(collect));
    }

    /**
     * 修改数据
     *
     * @param collect 实体对象
     * @return 修改结果
     */
    @PutMapping
    public R update(@RequestBody Collect collect) {
        return R.success(this.collectService.updateById(collect));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public R delete(@RequestParam("idList") List<Long> idList) {
        return R.success(this.collectService.removeByIds(idList));
    }

    @PostMapping("/add")
    public R addCollect(@RequestBody Collect collect) {
        // 检查是否已经收藏过该歌曲
        long count = collectService.count(new LambdaQueryWrapper<Collect>()
                .eq(Collect::getUserId, collect.getUserId())
                .eq(Collect::getSongId, collect.getSongId())
        );
        if (count > 0) {
            return R.warning("已经收藏过该歌曲");
        }

        // 添加收藏
        boolean result = collectService.save(collect);
        if (result) {
            return R.success("收藏成功");
        } else {
            return R.success("收藏失败");
        }
    }

    /**
     * 删除收藏记录
     *
     * @param collect 请求体参数，包含用户ID和歌曲ID
     * @return R 返回操作结果，包含成功或失败的消息及布尔值
     */
    @PostMapping("/delete")
    public R delete(@RequestBody Collect collect) {
        boolean remove = collectService.remove(new LambdaQueryWrapper<Collect>()
                .eq(Collect::getUserId, collect.getUserId())
                .eq(Collect::getSongId, collect.getSongId())
        );
        return remove ? R.success("取消收藏成功", true) : R.success("取消收藏失败", false);
    }

    //get(`collect/status?userId=${userId}&type=${type}&songId=${songId}`
    @GetMapping("/status")
    public R getCollectStatus(@RequestParam("userId") Long userId,
                              @RequestParam("type") Integer type,
                              @RequestParam("songId") Long songId) {
        long count = collectService.count(new LambdaQueryWrapper<Collect>()
                .eq(Collect::getUserId, userId)
                .eq(Collect::getType, type)
                .eq(Collect::getSongId, songId)
        );
        return count > 0 ? R.success("已收藏", true) : R.success("尚未收藏", false);
    }


}
